package com.shiro.test.mvc.controller;

import com.shiro.test.mvc.pojo.Menu;
import com.shiro.test.mvc.pojo.MenuPermission;
import com.shiro.test.mvc.pojo.User;
import com.shiro.test.mvc.service.IMenuService;
import com.shiro.test.mvc.service.IRoleService;
import com.shiro.test.mvc.service.IUserService;
import com.shiro.test.mvc.util.Constants;
import com.shiro.test.mvc.util.MessageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/11/27 0027.
 */
@Controller
@RequestMapping("user.html")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping(params = "act=page")
    @RequiresPermissions("4")
    public String page(){
        return "user_list";
    }

    @RequestMapping(params = "act=go_edit")
    public String go_edit(){
        return "user_edit";
    }

    @RequestMapping(params = "act=list")
    @ResponseBody
    public List<User> getUserList(){
        return userService.getUserList();
    }

    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map<String,Object> doEdit(User user){
        if (user.getId() == null){
            userService.insert(user);
        }else {
            userService.updateByPrimaryKey(user);
        }

        return MessageUtil.getSuccessResult("编辑成功");
    }

    @RequestMapping(params = "act=user_role")
    @ResponseBody   //该注解限制返回的数据时JSON格式。
    public List<Integer> userRole(int userId){
        return userService.getUserRoles(userId);
    }

    @RequestMapping(params = "act=assign_role")
    @ResponseBody   //该注解限制返回的数据时JSON格式。
    public Map<String,Object> assignMenu(int userId,int[] roleIds){
        userService.addUserRole(userId,roleIds);
        return MessageUtil.getSuccessResult("编辑成功");
    }

}
